package com.example.day02.luckfrag;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.example.day02.R;
import com.example.day02.startfrag.StarInfo;
import com.example.day02.utils.AssetsUtils;
import com.google.gson.Gson;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class LuckFragment extends Fragment {
    GridView luckGv;
    private LuckBaseAdapter adapter;

    public LuckFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_luck, container, false);
        luckGv=view.findViewById(R.id.luck_gv);
        //        读取assets/xzcontent/xzcontent.json文件
        String json = AssetsUtils.getJsonByAssets(getContext(), "xzcontent/xzcontent.json");
//        将json字符串转换成创建类的对象
        Gson gson = new Gson();
        StarInfo starInfo = gson.fromJson(json, StarInfo.class);
        List<StarInfo.StarinfoBean> list = starInfo.getStarinfo();  //数据源
//        创建适配器
        adapter = new LuckBaseAdapter(getContext(), list);
        luckGv.setAdapter(adapter);
        return view;
    }
}
